package cn.com.mayue.guava.collections;

import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.PeekingIterator;
import org.junit.Test;

import java.util.List;

/**
 * Created by mayue on 2017/4/3.
 *
 */
public class PeekingIteratorTest {
    /**
     * 此demo，项目中直接使用set就搞定了
     * 此处纯属test
     */
    @Test
    public void test(){
        List<String> sourceList = Lists.newArrayList();
        sourceList.add("one");
        sourceList.add("two");
        sourceList.add("two");
        sourceList.add("three");
        List<String> result = Lists.newArrayList();
        PeekingIterator<String> iter = Iterators.peekingIterator(sourceList.iterator());
        while (iter.hasNext()) {
            String current = iter.next();
            while (iter.hasNext() && iter.peek().equals(current)) {
                //跳过重复的元素
                iter.next();
            }
            result.add(current);
        }
        System.out.println(result);
    }
}
